package com.learn.demo.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.concurrent.atomic.AtomicLong;

/**
 * @author banjiawei
 * @date 2021/03/16
 * 埋点
 */
@Service
@EnableAsync
public class Schedule {
    private static AtomicLong atomicLong = new AtomicLong();

    @Autowired
    private CharMetrics charMetrics;

    /**
     * 计数器
     * @return
     */
    public static long addNext(){
        return atomicLong.getAndIncrement();
    }

    @Async
    @Scheduled(fixedDelay = 1000)
    public  void increment(){
        long count = addNext();
        charMetrics.counterOne.increment();
        charMetrics.counterThree.increment();
        charMetrics.counterThree.increment();
        charMetrics.map.put("x", count);
    }
}
